import 'dart:async';

import 'package:flutter/material.dart';

import '../common/flatPanel.dart';
import '../entry.dart';
import '../models/changeLogItem.dart';
import '../utils/screw.dart';

class ChangeLogView extends StatefulWidget {
  @override
  _ChangeLogViewState createState() {
    return _ChangeLogViewState();
  }
}

class _ChangeLogViewState extends State<ChangeLogView> {
  final entry = Entry.ChangeLog;
  final baseUrl = Entry.BaseUrl + Entry.ChangeLog;
  final dio = Screw.dioInstance;
  var _items = <ChangeLogItem>[];

  _getDetails() async {
    var data = await Screw.getList(baseUrl + '/all', entry, 'details');
    setState(() {
      _items = ChangeLogItem.genFromList(data);
    });
  }

  // 下拉刷新
  Future<void> _onRefresh() async {
    await Future.delayed(Duration(milliseconds: 400), () {
      _getDetails();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(Parts.ChangeLogView),
      ),
      body: FlatPanel(
        child: ListView.separated(
          itemCount: _items.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text('${_items[index].entry}'),
              subtitle: Text('${_items[index].body}'),
            );
          },
          separatorBuilder: (context, i) => Divider(),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.refresh),
        onPressed: _onRefresh,
      ),
    );
  }
}
